

#include "utils.h"
#include <sys/time.h>

static struct timeval _tv_start;


void utime_init()
{
	gettimeofday(&_tv_start, 0);
}
inline utime_t get_time() //!< ms
{
	struct timeval tv;
	gettimeofday(&tv, 0);
	return (tv.tv_sec - _tv_start.tv_sec) * TIME_GAP +
		((int)tv.tv_usec - (int)_tv_start.tv_usec)  / ((1000000 / TIME_GAP) + 1);
}

#if (TESTING & UTILS_TEST)

#include   <unistd.h>  

/** 
 * @name utils testsuit 
 * @brief 
 *
 * @{ */

int main(int argc, char * argv[])
{
	int i;

	DBG("perr test....\n");
	perr(SUCCESS);
	perr(ERROR);
	perr(FATAL_ERR);
	serr("error testing");
	perr(OTHER_RET);
	DBG("\n");

	unsigned int t1, t2;
	DBG("get_time test...\n");
	for(i = 0; i < 10; i++)
	{
		t1 = get_time();
		DBG("time: %u\n", t1);
		DBG("sleep 2 second...\n");
		sleep(2);
		t2 = get_time();
		DBG("time: %u\n", t2);
		if(t2 - t1 > 1999000 && t2 - t1 < 2001000)		//!< 1 ms
			DBG("get_time ........ OK\n");
		else
			DBG("get_time ........ERROR\n");
		DBG("\n");
	}

	return 0;
}

/**  @} */

#endif
